<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊医生" prop="doctorName">
        <el-input
            v-model="queryParams.doctorName"
            placeholder="请输入就诊医生"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="诊断描述" prop="treatInfo">-->
<!--        <el-input-->
<!--          v-model="queryParams.treatInfo"-->
<!--          placeholder="请输入诊断描述"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="药方Id" prop="prescriptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.prescriptId"-->
<!--          placeholder="请输入药方Id"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="就诊状态" style="width: 220px" prop="treatState">
        <el-select
            v-model="queryParams.treatState"
            placeholder="请选择就诊状态"
            clearable
            @keyup.enter="handleQuery"
        >
          <el-option label="待就诊" value="待就诊"></el-option>
          <el-option label="已就诊" value="已就诊"></el-option>
          <!-- 如果有其他状态，可以继续添加option -->
        </el-select>
      </el-form-item>
<!--      <el-form-item label="就诊时间" prop="treatTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.treatTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择就诊时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="Plus"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['behindTreatment:beYyyTreatment:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['behindTreatment:beYyyTreatment:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['behindTreatment:beYyyTreatment:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['behindTreatment:beYyyTreatment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="beYyyTreatmentList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="就诊Id" align="center" prop="treatId" />-->
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="就诊医生" align="center" prop="doctorName" />
<!--      <el-table-column label="预约id" align="center" prop="appId" />-->
      <el-table-column label="诊断描述" align="center" prop="treatInfo" />
<!--      <el-table-column label="药方Id" align="center" prop="prescriptId" />-->
<!--      <el-table-column label="就诊状态" align="center" prop="treatState" />-->
      <el-table-column label="就诊状态" align="center" prop="treatState">
        <template #default="scope">
          <!-- 根据不同状态显示不同颜色的标签 -->
          <el-tag
              :type="scope.row.treatState === '待就诊' ? 'warning' :
             scope.row.treatState === '已就诊' ? 'success' : 'info'"
          >
            {{ scope.row.treatState }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="预约时间" align="center" prop="appDateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.appDateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="就诊时间" align="center" prop="treatTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.treatTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template #default="scope">-->
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['behindTreatment:beYyyTreatment:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['behindTreatment:beYyyTreatment:remove']">删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改就诊总览对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="beYyyTreatmentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预约id" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入预约id" />
        </el-form-item>
        <el-form-item label="诊断描述" prop="treatInfo">
          <el-input v-model="form.treatInfo" placeholder="请输入诊断描述" />
        </el-form-item>
        <el-form-item label="药方Id" prop="prescriptId">
          <el-input v-model="form.prescriptId" placeholder="请输入药方Id" />
        </el-form-item>
        <el-form-item label="就诊状态" prop="treatState">
          <el-input v-model="form.treatState" placeholder="请输入就诊状态" />
        </el-form-item>
        <el-form-item label="就诊时间" prop="treatTime">
          <el-date-picker clearable
            v-model="form.treatTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择就诊时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BeYyyTreatment">
import { listBeYyyTreatment, getBeYyyTreatment, delBeYyyTreatment, addBeYyyTreatment, updateBeYyyTreatment } from "@/api/behindTreatment/beYyyTreatment"

const { proxy } = getCurrentInstance()

const beYyyTreatmentList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    appId: null,
    userName: null,
    doctorName: null,
    treatInfo: null,
    prescriptId: null,
    treatState: null,
    treatTime: null
  },
  rules: {
    appId: [
      { required: true, message: "预约id不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询就诊总览列表 */
function getList() {
  loading.value = true
  listBeYyyTreatment(queryParams.value).then(response => {
    beYyyTreatmentList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    treatId: null,
    appId: null,
    userName: null,
    doctorName: null,
    treatInfo: null,
    prescriptId: null,
    treatState: null,
    treatTime: null
  }
  proxy.resetForm("beYyyTreatmentRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.treatId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加就诊总览"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _treatId = row.treatId || ids.value
  getBeYyyTreatment(_treatId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改就诊总览"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["beYyyTreatmentRef"].validate(valid => {
    if (valid) {
      if (form.value.treatId != null) {
        updateBeYyyTreatment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBeYyyTreatment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _treatIds = row.treatId || ids.value
  proxy.$modal.confirm('是否确认删除就诊总览编号为"' + _treatIds + '"的数据项？').then(function() {
    return delBeYyyTreatment(_treatIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('behindTreatment/beYyyTreatment/export', {
    ...queryParams.value
  }, `beYyyTreatment_${new Date().getTime()}.xlsx`)
}

getList()
</script>
